package com.action;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ServiceAAction {


    @Autowired
    private  RestTemplate  restTemplate;



    @GetMapping("/a_add")
    @HystrixCommand(fallbackMethod = "callback")
    public String a_add(int x , int y){

        //String url = "http://127.0.0.1:7002/add?x=%d&y=%d";
        //String newUrl = String.format(url,x,y);
        String url = "http://service-b/add?x=%d&y=%d";
        String newUrl = String.format(url,x,y);
        String result= restTemplate.getForObject(newUrl,String.class);
        return result;
    }

    public String callback(int x,int y){
        String url = "http://service-b/add?x=%d&y=%d";
        String newUrl = String.format(url,x,y);
        String result= restTemplate.getForObject(newUrl,String.class);
        return result;
    }


}
